<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] Declarations</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:12:30 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_16.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JLR2-CH-5">5. Declarations</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
Naming Conventions<br>
<A HREF="ch05_02.htm">Lexical Scope of Declarations</A><BR>
<A HREF="ch05_03.htm">Object-Orientation Java Style</A><BR>
<A HREF="ch05_04.htm">Class Declarations</A><BR>
<A HREF="ch05_05.htm">Interface Declarations</A><BR>

<p>
</DIV>

<P CLASS=para>
A declaration
is a construct that associates a name with storage that contains
specified data or a specified type of data. More specifically, declarations
associate names with classes, interfaces, methods, and variables.
In addition, the declaration of a class, interface, or method defines
the actual class, interface, or method that is associated with the
name. Methods and variables can only be declared within classes
and interfaces, so this chapter covers method and variable declarations
in the context of class and interface declarations.

<P CLASS=para>
Every name has a <I CLASS=emphasis>lexical scope</I>. The scope of a declaration
determines the portions of a program in which the declaration
is applicable.

<P CLASS=para>
A declaration can be preceded by modifiers
that specify attributes of the name or of the data associated with
the name. One such attribute for a name is its accessibility. The
accessibility modifiers specify the other classes that can access
the data associated with the name. The <tt CLASS=literal>static</tt>
modifier specifies an attribute for data; it indicates whether the
data is associated with a class or with individual instances of
a class.

<P CLASS=para>
Because Java is an object-oriented programming
language, this chapter also describes the object-oriented model
used by the language. An understanding of this model is necessary
for a complete understanding of class and interface declarations.

<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-5-SECT-1">5.1 Naming Conventions</A></h2>

<P CLASS=para>
The Java language
has no requirements for choosing names, aside from the lexical requirements
for identifiers stated
in <A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.1">Identifiers</A>.
However, there are certain
conventions that you should follow when choosing names; these conventions
are the ones used by Sun in much of the Java API. Following these
conventions makes your programs easier to read, as many programmers
are already accustomed to reading programs that use them:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If an identifier is logically made up of multiple words, the first
letter of each word other than the first is uppercase and the rest of
the letters are lowercase (e.g., <tt CLASS=literal>aSimpleExample</tt>).
Sun is consistent about following this convention.

<P>
<li CLASS=listitem>The first letter of the name of a class or interface is uppercase,
while the first letter of all other names is lowercase. Sun is also
consistent about following this convention.

<P>
<li CLASS=listitem>The names of final variables that are intended to represent symbolic
constants are all uppercase; logical words contained in the name are
separated by underscore characters (e.g.,
<tt CLASS=literal>MAX_LEGAL_VALUE</tt>).  Sun uses this convention quite
often, but is not entirely consistent.

<P>
<li CLASS=listitem>Some Java programmers have adopted the additional convention
of beginning the names of instance variables with an underscore
(e.g., <tt CLASS=literal>_value</tt>).

<P>
<li CLASS=listitem>Avoid the use of <tt CLASS=literal>$</tt> in names to prevent confusion with compiler-generated names.  Sun is consistent about following this convention.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.2">Class Name</A>;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.1">Identifiers</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5.2">Interface Name</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5.4.1">Interface Variables</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.1">Variables</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_16.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Constant Expressions</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Lexical Scope of Declarations</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
